﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace HuiZe.education.View.Ethan
{
    /// <summary>
    /// PART_Media_Video.xaml 的交互逻辑
    /// </summary>
    public partial class PART_Media_Video : UserControl
    {
        public bool IsAutoPlay { get; set; }


        public PART_Media_Video()
        {
            InitializeComponent();
            _is_opended = false;
            PART_Play.Click += PART_Play_Click;
            PART_Volume.Click += PART_Volume_Click;
            MouseEnter += PART_Player_MouseEnter;
            MouseLeave += PART_Player_MouseLeave;
            PART_FullScreen.Click += PART_FullScreen_Click;
            PART_Player.MediaOpened += PART_Player_MediaOpened;
            PART_Play_Continue.Click += PART_Play_Continue_Click;
            PART_Panel_Volume.MouseLeave += PART_Panel_Volume_MouseLeave;
            PART_Slider_Volume.ValueChanged += PART_Slider_Volume_ValueChanged;
            PART_Slider_Position.ValueChanged += PART_Slider_Position_ValueChanged;
        }

        public void PlayMp4()
        {
            PART_Play_Click(this, new RoutedEventArgs());
        }

        void PART_Play_Continue_Click(object sender, RoutedEventArgs e)
        {
            Play();
        }

        void PART_Panel_Volume_MouseLeave(object sender, MouseEventArgs e)
        {
            PART_Panel_Volume.Visibility = Visibility.Collapsed;
        }

        void PART_FullScreen_Click(object sender, RoutedEventArgs e)
        {
            if (PART_FullScreen.Content.ToString() == "全屏")
            {
                PART_FullScreen.Content = "退出全屏";
                if (_fullscreen_callback != null)
                    _fullscreen_callback(true);
            }
            else
            {
                PART_FullScreen.Content = "全屏";
                if (_fullscreen_callback != null)
                    _fullscreen_callback(false);
            }
        }

        void PART_Player_MouseLeave(object sender, MouseEventArgs e)
        {
            PART_Panel_Volume.Visibility = Visibility.Collapsed;
            PART_Panel_Controller.Visibility = Visibility.Hidden;
        }

        void PART_Player_MouseEnter(object sender, MouseEventArgs e)
        {
            PART_Panel_Controller.Visibility = Visibility.Visible;
        }

        public void SetVideo(string _path, Action<bool> _callback_fullscreen = null)
        {
            _path_src = _path;
            _is_pause = true;
            _position_all = string.Empty;
            _fullscreen_callback = _callback_fullscreen;
            _is_opended = string.IsNullOrEmpty(_path_lastest) || _path_lastest != _path;
            PART_Player.Source = new Uri(_path_src, UriKind.Absolute);
            if (!_is_opended)
                PART_Player.Play();

            _path_lastest = _path;
        }

        void PART_Volume_Click(object sender, RoutedEventArgs e)
        {
            PART_Panel_Volume.Visibility = Visibility.Visible;
        }

        void PART_Play_Click(object sender, RoutedEventArgs e)
        {
            if (_is_pause)
            {
                _is_pause = false;
                Play();
            }
            else
            {
                _is_pause = true;
                Pause();
            }
            e.Handled = true;
        }

        void PART_Slider_Volume_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            PART_Player.Volume = e.NewValue;
        }

        void PART_Slider_Position_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            PART_Player.Position = TimeSpan.FromSeconds(e.NewValue);
        }

        void PART_Player_MediaOpened(object sender, RoutedEventArgs e)
        {
            if (PART_Slider_Volume.Value == 0.0) PART_Slider_Volume.Value = 0.5;
            PART_Slider_Position.Maximum = PART_Player.NaturalDuration.TimeSpan.TotalSeconds;
            _position_all = $"{PART_Player.NaturalDuration.TimeSpan.Minutes.ToString("D2")}:{PART_Player.NaturalDuration.TimeSpan.Seconds.ToString("D2")}";

            PART_Position.Text = $"00:00 / {_position_all}";
            PART_Player.Stop();

            if (IsAutoPlay)
                PlayMp4();

            _is_opended = true;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            PART_Slider_Position.Value = PART_Player.Position.TotalSeconds;
            PART_Position.Text = $"{PART_Player.Position.Minutes.ToString("D2")}:{PART_Player.Position.Seconds.ToString("D2")} / {_position_all}";
        }

        void Play()
        {
            _is_pause = false;
            PART_Play.Content = "暂停";
            PART_Player.Play();
            if (_timer == null)
            {
                _timer = new DispatcherTimer(DispatcherPriority.Background);
                _timer.Interval = TimeSpan.FromSeconds(1);
                _timer.Tick += _timer_Tick;
            }
            if (_timer != null)
                _timer.Start();
            PART_Play_Continue.Visibility = Visibility.Collapsed;
        }

        void Pause()
        {
            _is_pause = true;
            PART_Play.Content = "播放";
            PART_Player.Pause();
            if (_timer != null)
                _timer.Stop();
            PART_Play_Continue.Visibility = Visibility.Visible;
        }

        public void Reset()
        {
            _is_pause = true;
            PART_Player.Stop();
            PART_Play.Content = "播放";
            PART_Play_Continue.Visibility = Visibility.Collapsed;
            PART_Slider_Position.Value = 0.0;
            if (_timer != null)
            {
                _timer.Stop();
                _timer.IsEnabled = false;
                _timer = null;
            }
        }


        bool _is_pause;
        bool _is_opended;
        string _path_src;
        string _position_all;
        string _path_lastest;
        DispatcherTimer _timer;
        Action<bool> _fullscreen_callback;
    }
}
